rm(list=ls())
require(tidyverse)
require(plyr)
require(dplyr)
require(scales)
require(rmeta)
files = list.files('results')[grepl("_nonwhite",list.files('results'))]


results = as.data.frame(matrix(nrow = 0, ncol = 7))
colnames(results) = c("coef", "se", "race", "dv", "p value", "endog", "state")



for(f in files){
  load(paste0("results/", f))
  holder = as.data.frame(matrix(nrow = 14, ncol = 7))
  colnames(holder) = c("coef", "se", "race", "dv", "p value", "endog", "state")
  
  holder$coef = c(ivmod1_white$cace, ivmod1_nonwhite$cace,
                   ivmod2_white$cace, ivmod2_nonwhite$cace,
                   ivmod3_white$cace, ivmod3_nonwhite$cace,
                   ivmod4_white$cace, ivmod4_nonwhite$cace,
                   ivmod5_white$cace, ivmod5_nonwhite$cace,
                   ivmod6_white$cace, ivmod6_nonwhite$cace,
                   ivmod_rep_white$cace, ivmod_rep_nonwhite$cace
                   
                   
                   
                   
                   
                   )
  
  holder$se = c(ivmod1_white$se, ivmod1_nonwhite$se, 
                ivmod2_white$se, ivmod2_nonwhite$se, 
                ivmod3_white$se, ivmod3_nonwhite$se,
                ivmod4_white$se, ivmod4_nonwhite$se, 
                ivmod5_white$se, ivmod5_nonwhite$se, 
                ivmod6_white$se, ivmod6_nonwhite$se, 
                ivmod_rep_white$se, ivmod_rep_nonwhite$se
                
                
                
                 
                 
                 
  )
  
  holder$`p value` = c(ivmod1_white$summary$coefficients[2,4], ivmod1_nonwhite$summary$coefficients[2,4],
                       ivmod2_white$summary$coefficients[2,4], ivmod2_nonwhite$summary$coefficients[2,4],
                       ivmod3_white$summary$coefficients[2,4], ivmod3_nonwhite$summary$coefficients[2,4], 
                       ivmod4_white$summary$coefficients[2,4], ivmod4_nonwhite$summary$coefficients[2,4], 
                       ivmod5_white$summary$coefficients[2,4], ivmod5_nonwhite$summary$coefficients[2,4], 
                       ivmod6_white$summary$coefficients[2,4], ivmod6_nonwhite$summary$coefficients[2,4], 
                       ivmod_rep_white$summary$coefficients[2,4], ivmod_rep_nonwhite$summary$coefficients[2,4]
                       
                       
                       
                        
                        
  )
  ############itt
 
  ##############
  holder$race = c(rep(c("white", "non-white"), 7))
  
  holder$dv = c(c(rep("Vote 2016", 2),
                 rep("Vote 2014", 2),
                 rep("Vote 2012", 2),
                 rep("Vote 2010", 2),
                 rep("Democrat", 2),
                 rep("Registered", 2),
                 rep("Republican", 2))
                 
                 )
  
  
  holder$endog = c(rep("Vote 2008", 7*2))
  holder$state = sub("results_","", sub("_nonwhite.Rdata", "", f))

  
  results = rbind(results, holder)
  
  print(f); print(nrow(results))
}





  
data = results %>%
  mutate(race=ifelse(race=='white', 'White', 'Non-white'))

pid.states = read_csv('state_fips.csv')%>%
  filter(record_pid==1)%>%
  pull(state_abbr)%>%
  tolower()


ggsave(filename = 'figures/FigS27.pdf', width = 9, height = 5, 
plot = data %>%
  filter(endog=='Vote 2008', grepl('Vote', dv))%>%
  ggplot(aes(x=coef, y = ..scaled..,weight = 1/(se^2), group = race, fill = race,linetype=race))+
  geom_density(alpha=.5)+
  xlab('State-Level Effects')+
  theme_minimal()+
  scale_linetype_manual(values = c( 'solid', 'dashed'))+
  scale_fill_manual(values = c('black','white'))+
  xlim(-0.1,.5)+
  ylab('Density')+
  theme(legend.title=element_blank(), legend.position = 'bottom', legend.spacing.x = unit(.25, 'cm'),
        axis.title.y=element_blank(), axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+  facet_wrap(dv ~ ., nrow = 4)
)

ggsave(filename = 'figures/FigS29.pdf', width = 9, height = 5, 
plot = data %>%
  filter(endog=='Vote 2008', !grepl('Vote', dv))%>%
  mutate(dv = factor(dv, levels = c('Registered','Democrat', 'Republican')),
         coef = ifelse(dv %in% c('Democrat', 'Republican') & !state %in% pid.states, NA, coef),
         se = ifelse(dv %in% c('Democrat', 'Republican') & !state %in% pid.states, NA, se))%>%
  ggplot(aes(x=coef, y = ..scaled..,weight = 1/(se^2), group = race, fill = race,linetype=race))+
  geom_density(alpha=.5)+
  xlab('State-Level Effects')+
  theme_minimal()+
  scale_linetype_manual(values = c( 'solid', 'dashed'))+
  scale_fill_manual(values = c('black','white'))+
  xlim(-0.1,.5)+
  ylab('Density')+
  theme(legend.title=element_blank(), legend.position = 'bottom', legend.spacing.x = unit(.25, 'cm'),
        axis.title.y=element_blank(), axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+  facet_wrap(dv ~ ., nrow = 4)
)



# Weighted average

# registration weighted average effect
predictions_white = as.data.frame(matrix(NA, ncol = 5))
predictions_nonwhite = as.data.frame(matrix(NA, ncol = 5))


colnames(predictions_white) = c("ev", "dv", "coef", "se", "race")
colnames(predictions_nonwhite) = c("ev", "dv", "coef", "se", "race")





data = results %>%
  filter(!dv %in% c('Democrat','Republican'))


for(i in 1:length(unique(data$endog))){
  for(j in 1:length(unique(data$dv))){
    if(length(data$dv[data$dv == unique(data$dv)[j] & data$endog==unique(data$endog)[i]]) > 0){
      
      preds_white = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "white"  & data$dv == unique(data$dv)[j] ], data$se[data$endog == unique(data$endog)[i] & data$race == "white" & data$dv == unique(data$dv)[j]])
      predictions_white = rbind(predictions_white, c(unique(data$endog)[i], unique(data$dv)[j], preds_white$est, preds_white$se, "white"))
      
      preds_nonwhite = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "non-white" & data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "non-white" & data$dv == unique(data$dv)[j]])
      predictions_nonwhite = rbind(predictions_nonwhite, c(unique(data$endog)[i], unique(data$dv)[j], preds_nonwhite$est, preds_nonwhite$se, "non-white"))
   
      
      
      
    }
    
  }
  
}
meta_plot=rbind(predictions_white, predictions_nonwhite)
for(i in 3:(ncol(meta_plot)-2)){
  meta_plot[,i] = as.numeric(meta_plot[,i])
}
meta_plot$se =as.numeric(meta_plot$se)
meta_plot = meta_plot[!is.na(meta_plot$ev),]
meta_plot = meta_plot[!is.nan(meta_plot$coef),]



plt1 = meta_plot%>%
  mutate(race = factor(Hmisc::capitalize(race), levels = c('Non-white', 'White')))


predictions_white = as.data.frame(matrix(NA, ncol = 5))
predictions_nonwhite = as.data.frame(matrix(NA, ncol = 5))


colnames(predictions_white) = c("ev", "dv", "coef", "se", "race")
colnames(predictions_nonwhite) = c("ev", "dv", "coef", "se", "race")




data = results %>%
  filter(state %in% pid.states & dv %in% c('Democrat', 'Republican'))


for(i in 1:length(unique(data$endog))){
  for(j in 1:length(unique(data$dv))){
    if(length(data$dv[data$dv == unique(data$dv)[j] & data$endog==unique(data$endog)[i]]) > 0){
      
      preds_white = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "white"  & data$dv == unique(data$dv)[j] ], data$se[data$endog == unique(data$endog)[i] & data$race == "white" & data$dv == unique(data$dv)[j]])
      predictions_white = rbind(predictions_white, c(unique(data$endog)[i], unique(data$dv)[j], preds_white$est, preds_white$se, "white"))
      
      preds_nonwhite = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "non-white" & data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "non-white" & data$dv == unique(data$dv)[j]])
      predictions_nonwhite = rbind(predictions_nonwhite, c(unique(data$endog)[i], unique(data$dv)[j], preds_nonwhite$est, preds_nonwhite$se, "non-white"))
      
      
      
      
      
    }
    
  }
  
}
meta_plot=rbind(predictions_white, predictions_nonwhite)
for(i in 3:(ncol(meta_plot)-2)){
  meta_plot[,i] = as.numeric(meta_plot[,i])
}
meta_plot$se =as.numeric(meta_plot$se)
meta_plot = meta_plot[!is.na(meta_plot$ev),]
meta_plot = meta_plot[!is.nan(meta_plot$coef),]



plt2 = meta_plot%>%filter(ev == "Vote 2008" )%>%
  mutate(race = factor(Hmisc::capitalize(race), levels = c('Non-white', 'White')))

plt = rbind(plt1,plt2)

ggsave(filename = "figures/FigS26.pdf", width=9,height=5,
plot = ggplot(data = plt[!plt$dv %in% c("Registered", 'Democrat', 'Republican'),], aes(x = dv, y = coef, color = race, fill = race)) +
  geom_hline(yintercept = 0, linetype = 'dotted', color = 'grey60') +
  geom_point(shape = 21, size = 6, position=position_dodge(.5), alpha = .75) + xlab('') +
  ylab('Effect of 2008 Voting on Future Voting') + 
  geom_errorbar(aes(ymin=coef-1.96*se, ymax=coef+1.96*se, linetype = race), width=0,
                position=position_dodge(.5)) +
  scale_color_manual(values = c("black", "black"))+
  scale_fill_manual(values = c("black", "white"))+
  scale_linetype_manual(values = c('solid', 'dotdash', 'dashed'))+
  theme_minimal()+
  scale_x_discrete(labels = c("2010", "2012", '2014', '2016'))+
  theme(axis.text.y=element_text(face='bold', size =14), axis.title.y=element_text(face='bold', size =14), 
        axis.text.x = element_text(face="bold", size =14), legend.text=element_text(face='bold', size =14),
        legend.position="bottom", legend.spacing.x = unit(.25, 'cm'),legend.title = element_blank(), legend.key.width=unit(4,"line"))+
  NULL
)

ggsave(filename = "figures/FigS28.pdf", width=9,height=5,
plot = ggplot(data = plt[plt$dv %in% c("Registered", 'Democrat', 'Republican'),]%>%
         mutate(dv = factor(dv, levels = c('Registered', 'Democrat', 'Republican'))), aes(x = dv, y = coef, color = race, fill = race)) +
  geom_hline(yintercept = 0, linetype = 'dotted', color = 'grey60') +
  geom_point(shape = 21, size = 6, position=position_dodge(.5), alpha = .75) + xlab('') +
  ylab('Effect of 2008 Voting on Registration') + 
  geom_errorbar(aes(ymin=coef-1.96*se, ymax=coef+1.96*se, linetype = race), width=0,
                position=position_dodge(.5)) +
  scale_color_manual(values = c("black", "black", "black"))+
  scale_fill_manual(values = c("black", "white", "grey"))+
  scale_linetype_manual(values = c('solid', 'dotdash', 'dashed'))+
  theme_minimal()+
  scale_x_discrete(labels = c("Registered", "Democrat", 'Republican'))+
  theme(axis.text.y=element_text(face='bold', size =14), axis.title.y=element_text(face='bold', size =14), 
        axis.text.x = element_text(face="bold", size =14), legend.text=element_text(face='bold', size =14),
        legend.position="bottom", legend.spacing.x = unit(.25, 'cm'),legend.title = element_blank(), legend.key.width=unit(4,"line"))+
  NULL
)


